<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of gidaff_upload
 *
 * @author QUYET
 */
class gidaff_upload {

    var $upload_path = '';
    var $download_path = '';
    var $upload_data = array();
    var $upload_error = array();

    function __construct() {
        
    }

    function initialize($config=array()) {
        if (array_key_exists('upload_path', $config)) {
            $this->upload_path = $config['upload_path'];
        }
        if (array_key_exists('upload_path', $config)) {
            $this->download_path = $config['download_path'];
        }
    }

    function do_upload() {
        $htmldata = array();
        $posted_data = $_POST;
        $fieldName = "flashUploadFiles"; //Filedata";
        if (isset($_FILES['uploadedfiles'])) {
            $cnt = 0;
            $len = count($_FILES['uploadedfiles']['name']);
            for ($i = 0; $i < $len; $i++) {
                $moved = move_uploaded_file($_FILES['uploadedfiles']['tmp_name'][$i], $this->upload_path . $_FILES['uploadedfiles']['name'][$i]);
                if ($moved) {
                    $name = $_FILES['uploadedfiles']['name'][$i];
                    $file = $this->upload_path . $name;
                    $type = $this->getImageType($file);
                    try {
                        list($width, $height) = getimagesize($file);
                    } catch (Exception $e) {
                        $width = 0;
                        $height = 0;
                        $_post['filesInError'] = $name;
                    }

                    if (!$width) {
                        $_post['filesInError'] = $name;
                        $width = 0;
                        $height = 0;
                    }

                    $_post['file'] = $this->download_path . $name;
                    $_post['name'] = $name;
                    $_post['width'] = $width;
                    $_post['height'] = $height;
                    $_post['type'] = $type;
                    $_post['size'] = filesize($file);
                    $htmldata[$cnt] = $_post;
                } elseif (strlen($_FILES['uploadedfiles']['name'][$i])) {
                    $this->upload_error[$cnt] = array("ERROR" => "File could not be moved: " . $_FILES['uploadedfiles']['name'][$i]);
                    return false;
                }
                $cnt++;
            }
        } elseif (isset($_FILES['uploadedfile'])) {
            $m = move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $this->upload_path . $_FILES['uploadedfile']['name']);
            $name = $_FILES['uploadedfile']['name'];
            $file = $this->upload_path . $name;
            $type = $this->getImageType($file);
            try {
                list($width, $height) = getimagesize($file);
            } catch (Exception $e) {
                $width = 0;
                $height = 0;
            }
            $htmldata['file'] = $this->download_path . $name;
            $htmldata['name'] = $name;
            $htmldata['width'] = $width;
            $htmldata['height'] = $height;
            $htmldata['type'] = $type;
            $htmldata['size'] = filesize($file);
        } else {
            $this->upload_error['no_file'] = "You did not select a file to upload";
            return FALSE;
        }
        $this->upload_data = array('posted_data'=>$posted_data,'file_data'=>$htmldata);
        return TRUE;
    }

    function upload_data() {
        return $this->upload_data;
    }

    function display_errors() {
        $err = '<ul class = "upload_errors">';
        if (count($this->upload_error) > 0) {
            foreach ($this->upload_error as $error) {
                $err .= '<li class = error>' . $error . '</li>';
            }
        } else {
            return '';
        }
        $err .= '</ul>';
    }

    function getImageType($filename) {
        return strtolower(substr(strrchr($filename, "."), 1));
    }

}

?>
